package com.example.sport.controller;


import com.example.sport.domain.*;
import com.example.sport.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "管理员相关", description = "这是描述")
@RestController
@RequestMapping("/admin")
@Validated
@Slf4j
//请求值校验
public class AdminController {


    @Autowired
    private EventService eventService;

    @Operation(summary = "赛事查询")
    @GetMapping("/college_event")
    public Result<List<Event>> getCollegeEvent(
            @RequestParam(required = false, defaultValue = "") String name) {
        //查询用户
        List<Event> list = eventService.getEventByEventName(name);
        return Result.success(list);
    }

    @Operation(summary = "赛事信息查询")
    @GetMapping("/event_info")
    public Result<Event> eventInfo(@RequestParam(required = false, defaultValue = "") String id) {
        Event event = eventService.getEventInfoById(id);
        return Result.success(event);
    }

    @Operation(summary = "赛事更新/添加")
    @PutMapping("/update_event")
    public Result updateEvent(@RequestBody Event event) {
        if (eventService.updateEvent(event) < 1) {
            eventService.addEvent(event);
            return Result.success("添加成功");
        }
        return Result.success("修改成功");
    }

    @Operation(summary = "赛事删除")
    @DeleteMapping("/delete_event")
    public Result deleteEvent(@RequestParam String id) {
        if (eventService.deleteEventById(id) > 0) {
            return Result.success("删除成功");
        }
        return Result.error("修改失败");
    }

    @Autowired
    private CollegeService collegeService;

    @Operation(summary = "学院列表查询")
    @GetMapping("/college")
    public Result<List<College>> getCollege(
            @RequestParam(required = false, defaultValue = "") String name) {
        //学院列表查询
        List<College> list = collegeService.getEventByCollegeName(name);
        return Result.success(list);
    }

    @Operation(summary = "学院信息查询")
    @GetMapping("/college_info")
    public Result<College> collageInfo(@RequestParam(required = false, defaultValue = "") String id) {
        College college = collegeService.getCollegeInfoById(id);
        return Result.success(college);
    }

    @Operation(summary = "学院更新/添加")
    @PutMapping("/update_college")
    public Result updateCollage(@RequestBody College college) {
        if (collegeService.updateCollage(college) < 1) {
            collegeService.addCollage(college);
            return Result.success("添加成功");
        }
        return Result.success("修改成功");
    }

    @Operation(summary = "学院删除")
    @DeleteMapping("/delete_college")
    public Result deleteCollege(@RequestParam String id) {
        if (collegeService.deleteEventById(id) > 0) {
            return Result.success("删除成功");
        }
        return Result.error("修改失败");
    }

    @Autowired
    private AthleteService athleteService;

    @Operation(summary = "运动员查询")
    @GetMapping("/athlete")
    public Result<List<User>> athlete(@RequestParam(required = false, defaultValue = "") String name) {
        return Result.success(athleteService.getUserDetailListByRoleAndName(name, "ATHLETE"));
    }

    @Operation(summary = "运动员信息查询")
    @GetMapping("/athlete_info")
    public Result<User> athleteInfoById(@RequestParam(required = false, defaultValue = "") String id) {
        return Result.success(athleteService.getUserById(id));
    }
    @Operation(summary = "运动员信息修改")
    @PutMapping("/update_athlete")
    public Result<String> updateAthlete(@RequestBody  User user) {
        athleteService.updateAthlete(user);
//        if (athleteService.updateAthlete(user) < 1) {
//            athleteService.addCollage(user);
//            return Result.success("添加成功");
//        }
        return Result.success("修改成功");


    }

}

